{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 创建第一个视觉程序“Hello，world！”，显示Lena图片。具体效果参看课程PPT。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "\n",
    "img = cv.imread(\"data/lena.jpg\")\n",
    "cv.imshow(\"Hello, Lena!\", img)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"data/cv_test_2.png\">运行结果</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 对Lena图像，分解得到RGB分量及HSV分量，显示各分量，并对结果进行比较说明。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding:utf8\n",
    "import cv2 as cv\n",
    "from functools import reduce\n",
    "\n",
    "img = cv.imread(\"data/lena.jpg\")\n",
    "cv.imshow(\"Original\", img)\n",
    "\n",
    "# 灰度图\n",
    "gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "cv.imshow(\"Gray\", gray)\n",
    "\n",
    "# 分解得到R,G,B分量, 显示各分量\n",
    "cv.waitKey()\n",
    "B, G, R = cv.split(img)\n",
    "rgb = ((\"Red\", R), (\"Green\", G), (\"Blue\", B))\n",
    "reduce(lambda _, x: cv.imshow(x[0], x[1]), rgb, None)\n",
    "\n",
    "cv.waitKey()\n",
    "reduce(lambda _, x: cv.destroyWindow(x[0]), rgb, None)\n",
    "\n",
    "# 分解得到H,S,V分量, 显示各分量\n",
    "img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)\n",
    "H, S, V = cv.split(img_hsv)\n",
    "hsv = ((\"H\", H), (\"S\", S), (\"V\", V))\n",
    "reduce(lambda _, x: cv.imshow(x[0], x[1]), hsv, None)\n",
    "\n",
    "cv.waitKey()\n",
    "reduce(lambda _, x: cv.destroyWindow(x[0]), hsv, None)\n",
    "\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果:\n",
    "<a href=\"data/cv_test_3_gray.png\">灰度图</a><br/>\n",
    "<a href=\"data/cv_test_3_red.png\">R分量图</a>\n",
    "<a href=\"data/cv_test_3_green.png\">G分量图</a>\n",
    "<a href=\"data/cv_test_3_blue.png\">B分量图</a><br/>\n",
    "<a href=\"data/cv_test_3_h.png\">H分量图</a>\n",
    "<a href=\"data/cv_test_3_s.png\">S分量图</a>\n",
    "<a href=\"data/cv_test_3_v.png\">V分量图</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### RGB分量图与灰度图的对比说明:\n",
    "+ R分量显示红色分量，可以看到在原图中与红色越接近的颜色，在R分量图中越接近白色;\n",
    "+ G分量显示红色分量，可以看到在原图中与绿色越接近的颜色，在G分量图中越接近白色;\n",
    "+ B分量显示红色分量，可以看到在原图中与蓝色越接近的颜色，在B分量图中越接近白色;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### HSV分量的对比说明:\n",
    "+ H分量图中可以看出，像素的颜色值是按照原图中的色彩波长来分布的，其中偏红色的波长较长，在分量图中就显示的更接近黑色;\n",
    "+ S分量图中可以看出，像素的颜色值是按照原图中的色彩饱满程度来分布的，颜色越深和纯的区域，在分量图中就显示的更接近白色;\n",
    "+ V分量图中可以看出，像素的颜色值是按照原图中的各区域亮度情况来分布的，越鲜艳的区域，在分量图中就显示的更接近白色;\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
